//
//  ViewController.m
//  UI27_Block
//
//  Created by Gollum on 16/8/19.
//  Copyright © 2016年 Gollum. All rights reserved.
//

#import "ViewController.h"
#import "SecondViewController.h"
@interface ViewController ()
//声明block属性
@property (nonatomic,copy)void (^xxxblock)();
//block在方法中
- (void)handleData:(void (^)(void))handleData;

@property (retain, nonatomic) IBOutlet UILabel *taxtLabel;

@end

@implementation ViewController
- (void)handleData:(void (^)(void))handleData{
    [[NSURLSession sharedSession] dataTaskWithRequest:[NSURLRequest new] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        handleData();
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];

    //1.Block
    //Block 与函数相似,有固定功能的代码段, 消息传递机制之一
    
    //2.Block的基本语法
    //1>block的声明
    //类型 void(^)变量名是block
    //返回值类型 (^block名)(参数列表);
    void (^block)(void);  //相当于int block
    //2>block赋值
    //block变量 = ^(参数列表){固定的代码段};
    block = ^(void){
    
    
    };
    //3.block的集中形式
    //有参有返回
    int (^ sum)(int,int);
    sum = ^(int a,int b){
    
        return a + b;
    
    };
    //无参无返回
    void (^block1)(void);
    block1 = ^(void){
        self.view.backgroundColor = [UIColor orangeColor];
    };
    //无参无返回最简单写法
    void (^block2)() = ^{
    
    
    };
    
    
    
    
    //有参无返回
    void (^printNumber)(int);
    printNumber = ^(int a){
    
        NSLog(@"%d",a);
    
    };
    
    //无参有返回,无参数可以把(void)去掉
    NSString *(^returnString)();
    returnString = ^{
    
        return @"111";
    
    };
    //系统提供了代码段可以直接使用, inlineBlock
   // <#returnType#>(^<#blockName#>)(<#parameterTypes#>) = ^(<#parameters#>) {
  //      <#statements#>};
    
    //block调用
    sum(10,10);
    block1();
    printNumber(100);
    
    
    //系统中的block
    //1>模态
  //  [self presentViewController:[UIViewController new] animated:YES completion:^{}];
    //2>GCD
    //dispatch_sync(dispatch_get_global_queue(DISPATCH_TARGET_QUEUE_DEFAULT, 0), ^{  });/**
    //3.系统的网络请求
    //[[NSURLSession sharedSession] dataTaskWithURL:[NSURLRequest new] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {  }];
    
    
    
    //6.block写在属性和方法上
    //属性block修饰符使用copy
    self.xxxblock = block1;
    self.xxxblock();
    //带block的方法调用
    [self handleData:^{
        
    }];
    
    //7.block类型的修饰符
    //__block __weak __strong
    
    //1> __block 在ARC和MRC下有区别
    //ARC和MRC下都可以
    //__block 可以在block中修改外部的变量
    //ARC不能避免循环引用的问题,MRC可以
    int __block a = 0;
    __block int b = 0;

    void (^blockText)(void);
    blockText = ^{
    
        a = 100;
    };
    //2.__weak 弱引用 修饰block外的变量
    //   只能在ARC下使用,避免block的循环引用
    
    //3.__strong 强引用
}
- (IBAction)pushNextVC:(id)sender {
    SecondViewController *second = [SecondViewController new];
    SecondViewController *__block weakSecondVC = second;
    //值得传递,需要实现Block
    //告诉block,该指针为弱引用
    second.paseTitle = ^(NSString *title){
    //block会对内部所有的对象进行强引用
        self.taxtLabel.text = title;
weakSecondVC.view.backgroundColor = [UIColor greenColor];
    };
    [self.navigationController pushViewController:second animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
